package com.icode.web.validator;

import com.icode.core.dto.user.MemberDTO;
import com.icode.service.user.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-16
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
@Component
public class MemberFormValidator implements Validator {

    @Autowired
    private MemberService memberService;

    @Override
    public boolean supports(Class<?> clazz) {
        return MemberDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        MemberDTO memberDTO = (MemberDTO) target;
        String idCard = memberDTO.getIdCard();
        String phoneNum = memberDTO.getPhoneNum();
        String birthday = memberDTO.getBirthday();
        String name = memberDTO.getName();
        String telephone = memberDTO.getTelephone();
        if (Pattern.matches("[\\d]{17}[\\w]{1}|[\\d]{15}", idCard)) {
            if (Pattern.matches("[\\d]{17}[\\w]{1}", idCard)) {
                if (!idCardValidate(idCard)) {
                    errors.rejectValue("idCard", null, "身份证不合法!");
                }
            }
            if (!isIDCard(idCard, birthday)) {
                errors.rejectValue("idCard", null, "身份证与生日不相符!");
            }
        } else {
            errors.rejectValue("idCard", null, "身份证不合法!");
        }
        if (!Pattern.matches("[\\d]{11}|[\\d]{11}\\,[\\d]{11}", phoneNum)) {
            errors.rejectValue("phoneNum", null, "手机号不正确!");
        }
        if (!Pattern.matches("[\u4E00-\u9FA5]*[a-z]*", name)) {
            errors.rejectValue("name", null, "不能含有非法字符!");
        }
        if (!isNull(telephone)) {
            if (!isTelephone(telephone)) {
                errors.rejectValue("telephone", null, "输入有误!");
            }
        }
    }

    private boolean isTelephone(String telephone) {
        String[] array = telephone.split("\\-");
        if (Pattern.matches("[\\d]{3,5}", array[0]) && Pattern.matches("[\\d]{4,}", array[1])) {
            return true;
        }
        return false;
    }

    private static boolean isNull(String s) {
        if (!"".equals(s) || s != null) {
            return true;
        }
        return false;
    }

    private boolean isIDCard(String IDCard, String birthday) {
        String[] birthdays = birthday.split("-");
        String b = "";
        for (int i = 0; i < birthdays.length; i++) {
            b += birthdays[i];
        }
        if (b.equals(IDCard.substring(6, 14))) {
            return true;
        }
        return false;
    }

    private boolean idCardValidate(String idCard) {
        String num = lastNum(idCard);
        if (num.equals(idCard.substring(17))) {
            return true;
        }
        return false;
    }

    private String lastNum(String idCard) {
        String result = "";
        String str = idCard.substring(0, 17);
        char[] array = str.toCharArray();
        int[] data = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += Integer.parseInt(array[i] + "") * data[i];
        }
        int a = sum % 11;
        switch (a) {
            case 0:
                result = "1";
                break;
            case 1:
                result = "0";
                break;
            case 2:
                result = "x";
                break;
            case 3:
                result = "9";
                break;
            case 4:
                result = "8";
                break;
            case 5:
                result = "7";
                break;
            case 6:
                result = "6";
                break;
            case 7:
                result = "5";
                break;
            case 8:
                result = "4";
                break;
            case 9:
                result = "3";
                break;
            case 10:
                result = "2";
                break;
        }
        return result;
    }

}
